/**
 * Created by a1 on 2017/10/23.
 */
"use strict";
var fs = require('fs');
var formidable = require('formidable');
var common = require('../models/common.js');
var CategorysModel = require('../models/Categorys.js');//分类Schema
var BookModel = require('../models/Book.js');//书本Schema
var SubcateModel = require('../models/Subcate.js');//书本Schema

exports.categoryKey=function(req,res){
    if(req.params.key=="male"){
        res.render('category',{
            page:'male'
        });
    }else if(req.params.key=="female"){
        res.render('category',{
            page:'female'
        });
    }else{
        res.render('error',{page:'error'});
    }
};
exports.categoryDefail=function(req,res){
    res.render('category/detail',{
        page:'detail',
        catId:req.params.catid,
        subId:req.params.subid,
        type:req.params.type
    });
};
exports.categoryMale=function(req,res){
    res.render('category/content',{
        page:'content',
        catId:req.params.catid,
        type:req.params.type
    });
};
exports.getCateBook=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let type=fields.type;
        let catid=parseInt(fields.catid);
        let page=parseInt(fields.page);
        let pagenum=page*10;
        let obj={catId:catid};
        function getBook(obj,type){
            let filter=`{"${type}":-1}`;
            filter=JSON.parse(filter);
            var p1=new Promise(function(resolve,reject){
                BookModel.findConut(obj,function(err,count){
                    if(err){
                        reject(err);
                        return;
                    }
                    resolve(count);
                })
            });
            p1.then(function(count){
                BookModel.findLimitSkip(obj,10,pagenum,filter,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[],count:count});
                    }
                    res.json({code:1,msg:"成功",info:docs,count:count});
                });
            },function(err){
                console.log(err);
                res.json({code:0,msg:"失败",info:[],count:0});
            });
        }
        getBook(obj,type);
    });
};
exports.getCateSubBook=function(req,res){
    var form = new formidable.IncomingForm();//创建表单
    form.parse(req,function(err,fields,files){
        let type=fields.type;
        let subid=parseInt(fields.subid);
        let catid=parseInt(fields.catid);
        let page=parseInt(fields.page);
        let pagenum=page*10;
        let obj={catId:catid,subCateId:subid};
        function getBook(obj,type){
            let filter=`{"${type}":-1}`;
            filter=JSON.parse(filter);
            var p1=new Promise(function(resolve,reject){
                BookModel.findConut(obj,function(err,count){
                    if(err){
                        reject(err);
                        return;
                    }
                    resolve(count);
                })
            });
            p1.then(function(count){
                BookModel.findLimitSkip(obj,10,pagenum,filter,function(err,docs){
                    if(err){
                        res.json({code:0,msg:"失败",info:[],count:count});
                    }
                    res.json({code:1,msg:"成功",info:docs,count:count});
                });
            },function(err){
                console.log(err);
                res.json({code:0,msg:"失败",info:[],count:0});
            });
        }
        getBook(obj,type);
    });
};
exports.getmaleCate=function (req,res) {
    CategorysModel.findSortFn({},{'cgid':1},function(err,docs){
        if(err){
            res.json({"msg":'失败',"code":0,info:[]});
        }
        var subarr=[];
        (function bl(i){
            i++;
            if(i==docs.length){
                let counts=[];
                (function bl1(j){
                    j++;
                    if(j==docs.length){
                        res.json({"msg":'成功',"code":1,info:{cg:docs,subc:subarr,counts:counts}});
                        return;
                    }else{
                        BookModel.findConut({catId:docs[j]['cgid']},function(err,count){
                            if(err){
                                res.json({"msg":'失败',"code":0,info:[]});
                                return;
                            }
                            counts.push(count);
                            bl1(j);
                        });
                    }
                }(-1));
                return;
            }else{
                SubcateModel.find({'cgid':docs[i]['cgid']},function(err,docs1){
                    if(err){
                        res.json({"msg":'失败',"code":0,info:[]});
                        return;
                    }
                    subarr.push(docs1);
                    bl(i);
                });
            }
        }(-1));
    });
};